package org.budo.validation.validator.impl;

import java.lang.annotation.Annotation;

import javax.validation.constraints.Max;

import org.budo.validation.exception.BudoMaxValidationException;
import org.budo.validation.validator.BudoValidator;

/**
 * @author lmw
 */
public class MaxValidator implements BudoValidator {
    public void validate(Annotation annotation, Integer parameterIndex, String parameterName, Object argumentValue) {
        Max max = (Max) annotation;
        if (null != argumentValue && argumentValue instanceof Number) {
            double actuallyValue = ((Number) argumentValue).doubleValue();
            long maxValue = max.value();
            if (actuallyValue > maxValue) {
                throw new BudoMaxValidationException(parameterIndex, parameterName, max, actuallyValue);
            }
        }
    }
}